package support;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Degree
{
   private int location;
   
   private String newUniversity;
   private int university;
   private String newDiscipline;
   private int discipline;
   private String title;
   private String date;
   private double gpa;
   
   public Degree()
   {
      location = -1;
      newUniversity = "";
      university = -1;
      discipline = -1;
      newDiscipline = "";
      title = "";
      date = "";
      gpa = 0.0;
   }
   
   
   public void newUniversity(String s)
   {
	   newUniversity = s;
   }
   
   public void university(int id)
   {
      university = id;
   }
   
   public void discipline(int id)
   {
      discipline = id;
   }
   
   public void newDiscipline(String s)
   {
	   newDiscipline = s;
   }
   
   public void title(String s)
   {
      title = s;
   }
   
   public void date(String s)
   {
      date = s;
   }
   
   public void gpa(double d)
   {
      gpa = d;
   }
   
   public void location(int id)
   {
      location = id;
   }
   
   
   
   public int getLocation()
   {
      return location;
   }
   
   public int getUniversity()
   {
      return university;
   }
   
   public int getDiscipline()
   {
      return discipline;
   }
   
   public String getNewDiscipline()
   {
	   return newDiscipline;
   }
   
   public String getTitle()
   {
      return title;
   }
   
   public String getDate()
   {
      return date;
   }
   
   public Double getGpa()
   {
      return gpa;
   }
   
   public String getNewUniversity()
   {
	   return newUniversity;
   }
   
   public String getLocationName()
   {   
		Connection conn = null;
		PreparedStatement stm = null;
		ResultSet result = null;
		String returnedString = null;
		try 
		{
			Class.forName("org.postgresql.Driver");
			conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Graduate_application_database", "postgres", "postgres");
			conn.setAutoCommit(false);
			stm = conn.prepareStatement("SELECT * FROM countries_and_states WHERE cs_id = ? ");
			stm.setInt(1, location);
			result = stm.executeQuery();
			result.next();
			returnedString = result.getString("country_state");
			conn.commit();
			conn.setAutoCommit(true);
			result.close();
			stm.close();
			conn.close();
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
	    finally {
	        if (result != null) {
	            try {
	                result.close();
	            } 
	            catch (SQLException e) { }
	            result = null;
	        }
	        if (stm != null) {
	            try {
	                stm.close();
	            } 
	            catch (SQLException e) { }
	            stm = null;
	        }
	        if (conn != null) {
	            try {
	                conn.close();
	            } 
	            catch (SQLException e) { }
	            conn = null;
	        }
	    }
		
		return returnedString;	      
   }
   
   public String getUniversityName()
   {
	   if (getNewUniversity() != "")
		   return newUniversity;
	   
		Connection conn = null;
		PreparedStatement stm = null;
		ResultSet result = null;
		String returnedString = null;
		try 
		{
			Class.forName("org.postgresql.Driver");
			conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Graduate_application_database", "postgres", "postgres");
			conn.setAutoCommit(false);
			stm = conn.prepareStatement("SELECT * FROM universities WHERE u_id = ? ");
			stm.setInt(1, university);
			result = stm.executeQuery();
			result.next();
			returnedString = result.getString("university");
			conn.commit();
			conn.setAutoCommit(true);
			result.close();
			stm.close();
			conn.close();
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
	    finally {
	        if (result != null) {
	            try {
	                result.close();
	            } 
	            catch (SQLException e) { }
	            result = null;
	        }
	        if (stm != null) {
	            try {
	                stm.close();
	            } 
	            catch (SQLException e) { }
	            stm = null;
	        }
	        if (conn != null) {
	            try {
	                conn.close();
	            } 
	            catch (SQLException e) { }
	            conn = null;
	        }
	    }
		
		return returnedString;
   }

   public String getDisciplineName()
   {
	    if (getNewDiscipline() != "")
		   return newDiscipline;
	   
		Connection conn = null;
		PreparedStatement stm = null;
		ResultSet result = null;
		String returnedString = null;
		try 
		{
			Class.forName("org.postgresql.Driver");
			conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Graduate_application_database", "postgres", "postgres");
			conn.setAutoCommit(false);
			stm = conn.prepareStatement("SELECT * FROM disciplines WHERE d_id = ? ");
			stm.setInt(1, discipline);
			result = stm.executeQuery();
			result.next();
			returnedString = result.getString("discipline");
			conn.commit();
			conn.setAutoCommit(true);
			result.close();
			stm.close();
			conn.close();
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
	    finally {
	        if (result != null) {
	            try {
	                result.close();
	            } 
	            catch (SQLException e) { }
	            result = null;
	        }
	        if (stm != null) {
	            try {
	                stm.close();
	            } 
	            catch (SQLException e) { }
	            stm = null;
	        }
	        if (conn != null) {
	            try {
	                conn.close();
	            } 
	            catch (SQLException e) { }
	            conn = null;
	        }
	    }
		
		return returnedString;
   }

}